package com.jianglin.state2.controller;

import com.jianglin.state2.core.ResponsePack;
import com.jianglin.state2.dao.entity.Employee;
import com.jianglin.state2.dao.entity.Message;
import com.jianglin.state2.model.params.MessageAddParams;
import com.jianglin.state2.model.params.MessageUpdateParams;
import com.jianglin.state2.service.MessageService;
import com.jianglin.state2.util.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponsePack
@RequestMapping("/message")
@Api(tags = {"紧急通知"})
public class MessageController {

    private final MessageService messageService;

    @Autowired
    public MessageController(MessageService messageService){ this.messageService = messageService;}

    @GetMapping("/all")
    @ApiOperation("获取所有信息")
    public List<Message> getAll(){ return messageService.list(); }

    @GetMapping("/count")
    @ApiOperation("获取信息数")
    public Integer getCount(){
        return messageService.count();
    }

    @GetMapping("/messageId/{messageId}")
    @ApiOperation("根据信息编号查找信息")
    public Message getMessageById(@ApiParam("信息ID") @PathVariable Integer messageId){
        return messageService.listByMap(SqlUtil.getColumnMap("messageId",messageId)).get(0);
    }

    @PostMapping("/add")
    @ApiOperation("添加信息")
    public boolean saveMessage(@Validated @RequestBody MessageAddParams messageAddParams) {
        Message message = messageAddParams.convertOut();
        return messageService.save(message);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新信息")
    public boolean updateMessage(@ApiParam("信息编号")@PathVariable Integer id,
                                 @Validated @RequestBody MessageUpdateParams messageUpdateParams){
        Message message = messageUpdateParams.convertOut();
        message.setMessageId(id);
        return messageService.updateById(message);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除信息")
    public boolean deleteMessage(@ApiParam("信息编号") @PathVariable Integer id){return messageService.removeById(id);}
}